package com.hyst.web;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hyst.common.ResultBean;
import com.hyst.common.ResultPage;
import com.hyst.domain.Lesson;
import com.hyst.service.LessonService;

/**
 * @Description:课程controller
 * @author：dy 
 *@Date：2018年08月26日
 */
@RestController
@RequestMapping("lessons")
@CrossOrigin(maxAge = 3600, origins = "*")
public class LessonController {
	@Autowired
	private LessonService lessonService;
	
	@GetMapping
	public ResultPage<Lesson> getProjects(Lesson lesson) {
		return ResultPage.success(lessonService.list(lesson));
	}

	@PostMapping
	public ResultBean<Lesson> creat(final Lesson lesson) {
		return ResultBean.success(lessonService.insert(lesson));
	}

	/**
	 * @Description: 根据主键查询
	 * @param id
	 * @return ResultBean<Lesson>
	 */
	@GetMapping("{id}")
	public ResultBean<Lesson> getByPriKey(@PathVariable("id") final Integer id) {
		return ResultBean.success(lessonService.getByPriKey(id));
	}

	/**
	 * @Description: 根据主键更新全字段
	 * @param lesson
	 * @return ResultBean<Lesson>
	 */
	@PatchMapping("{id}")
	public ResultBean<Lesson> update(final Lesson lesson) {
		return ResultBean.success(lessonService.update(lesson));
	}

	/**
	 * @Description: 根据主键更新不为默认值的字段
	 * @param lesson
	 * @return ResultBean<Lesson>
	 */
	//@PutMapping("{id}")
	@PostMapping("{id}")
	public ResultBean<Lesson> updatePatch(@PathVariable("id") final Integer id,
			final Lesson lesson) {
		return ResultBean.success(lessonService.updateSelective(lesson));
	}

	/**
	 * @Description: 根据主键删除
	 * @param id
	 * @return ResultBean<Lesson>
	 */
	@DeleteMapping("{id}")
	public ResultBean<Lesson> delete(@PathVariable("id") final Integer id) {
		return ResultBean.success(lessonService.delete(id));
	}

	//绑定下拉用
	@GetMapping("select2")
	@ResponseBody
	public List<Map> getSelect2Data() {
		return lessonService.getSelect2Data();
	} 

	/**
	 * 获取含有级别的课程 
	* @Description: 
	* @return	ResultPage<Lesson>
	 */
	@GetMapping("hasLevel")
	@ResponseBody
	public ResultPage<Lesson> getLessonsHasLevel(){
		return ResultPage.success(lessonService.getLessonsHasLevel());
	}
}
